package com.siqi.hr.system.custom.service;

import com.siqi.hr.common.utils.Const;
import com.siqi.hr.system.common.enums.FieldPosition;
import com.siqi.hr.system.common.model.ExtraParameter;
import com.siqi.hr.system.common.sql.STable;
import com.siqi.hr.system.custom.dao.intf.FieldEmployeeDao;
import com.siqi.hr.system.custom.entity.FieldRecord;
import com.siqi.hr.system.organization.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 处理员工与字段布局、过滤条件布局关系通用service层
 * @author    xiaogq
 * @date         2016/8/24.
 */
@Service("fieldEmployeeService")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class FieldEmployeeService {

    @Autowired
    private FieldEmployeeDao fieldEmployeeDao;

    /**
     * 查询当前登录员工设置的查询条件字段通用方法
     * @author                   xiaogq
     * @param employee         当前登录系统用户信息
     * @param entityId         实体Id
     */
   /* public String queryEmployeeFilter(Employee employee, Integer entityId)throws Exception{
        ExtraParameter extraParam = new ExtraParameter();
        extraParam.setEntityId(entityId);
        extraParam.setTableName(STable.TABLE_HEAD + entityId);
        extraParam.setEmployee(employee);
        return fieldEmployeeDao.queryEmployeeFilter(extraParam);
    }*/

    /**
     * 弹窗查询方法
     * @author                   xiaogq
     * @param employee         当前登录系统用户信息
     * @param entityId         实体Id
     * @param isTable          是否是弹窗(true:是，false:列表数据)
     */
    public String queryEmployeeFilter(Employee employee, Integer entityId,boolean isTable)throws Exception{
        ExtraParameter extraParam = new ExtraParameter();
        extraParam.setEntityId(entityId);
        extraParam.setTableName(STable.TABLE_HEAD + entityId);
        extraParam.setEmployee(employee);
        extraParam.setTable(isTable);
        return fieldEmployeeDao.queryEmployeeFilter(extraParam);
    }

    /**
     * 获取客户化实体列头通用方法
     * @author                   xiaogq
     * @param emp（员工信息）
     * @param entityId（实体id）
     * @param showOperation（是否有操作列）
     * @param showCheckbox （是否显示复选框）
     * @throws Exception
     */
    public Map<String, Object> getEntityColumns(Employee emp, Integer entityId,Boolean showOperation,Boolean showCheckbox) throws Exception{
        return fieldEmployeeDao.getEntityColumns(emp,entityId,showOperation,showCheckbox);
    }

    /**
     * 获取实体已布局的字段信息，动态加载bootstrap表格
     */
    public List<FieldRecord> getFieldRecordList(Employee emp,Integer entityId) throws Exception{
        List<FieldRecord> fieldsList = fieldEmployeeDao.queryEntityLayoutField(emp,entityId, FieldPosition.List);
        if (fieldsList == null){
            fieldsList = new ArrayList<FieldRecord>();
        }
        //设置id字段
        FieldRecord idField = new FieldRecord();
        idField.setName("id");
        idField.setInchinese("id");
        //设置flag_bit(停用/启用)字段【注:所有的实体表必须有该字段，如果没有请添加】
        FieldRecord flagField = new FieldRecord();
        flagField.setName(Const.FLAG_BIT);
        flagField.setInchinese("停用标记");
        if (!fieldsList.contains(idField)) {//如果id没有在布局内则添加字段id
            fieldsList.add(0,idField);
        }
        if(!fieldsList.contains(flagField)){//如果flag_bit没有在布局内则添加字段停用标记字段
            fieldsList.add(flagField);
        }
        return fieldsList;
    }
}
